﻿Imports System.Text

Public Class clsReader
    Private _IP As String
    Private _PORT As String
    Private _QR_CODE As String
    Public Property IP As String
        Get
            Return _IP
        End Get
        Set(value As String)
            _IP = value
        End Set
    End Property

    Public Property PORT As String
        Get
            Return _PORT
        End Get
        Set(value As String)
            _PORT = value
        End Set
    End Property

    Public Property QR_CODE As String
        Get
            Return _QR_CODE
        End Get
        Set(value As String)
            _QR_CODE = value
        End Set
    End Property


    Dim intReaderNum As Integer = 1
    Protected WithEvents reader1 As clsEquipCooperateCommand

    Public Sub New(__IP As String, __PORT As String)
        Me.IP = __IP
        Me.PORT = __PORT

        reader1 = New clsEquipCooperateCommand(IP, PORT)
        reader1.CommandConnect()
        AddHandler reader1.tcpevent, AddressOf TCPCLEvent1
        System.Threading.Thread.Sleep(500)
    End Sub
    Delegate Sub TCPCLEventDelegate1(e As TCPListener.TCPEventArgs)
    Private Sub TCPCLEvent1(sender As System.Object, e As TCPListener.TCPEventArgs)
        Debug.WriteLine("TCPCLEvent in")
        If reader1.isConnected Then
            Dim adrData As New TCPCLEventDelegate1(AddressOf DoTCPCLEvent1)

        End If
    End Sub
    Public Sub read()
        'MsgBox(reader1.isConnected)
        reader1.CommandSend("start")
    End Sub
    Private Sub DoTCPCLEvent1(e As TCPListener.TCPEventArgs)
        ' 接続完了
        If e.eventType = TCPListener.TCPEventArgs.TcpEventType.Connect Then
            Debug.WriteLine("接続完了")
        End If
        If e.eventType = TCPListener.TCPEventArgs.TcpEventType.Shutdown Then
            Debug.WriteLine("接続切断")
        End If
        ' データ受信
        If e.eventType = TCPListener.TCPEventArgs.TcpEventType.Receive Then
            Debug.WriteLine("データ受信")
            Dim receiveDataStr = Encoding.ASCII.GetString(e.receiveData, 0, e.receiveData.Count())
            '<P>TP002</P>
            QR_CODE = ""
            If receiveDataStr.Equals("<p>NoRead</p>") = False Then
                Dim receiveDataStrReplace = receiveDataStr.Replace("<p>", "").Replace("</p>", "")
                'txtDL_NoA.Text = String.Empty
                QR_CODE = receiveDataStrReplace

            Else
                If intReaderNum < 6 Then
                    intReaderNum += 1
                    reader1.CommandSend("start")
                End If

            End If

            End If

        ' Exception
        If e.eventType = TCPListener.TCPEventArgs.TcpEventType.Exception Then
            Debug.WriteLine("Exception受信")
            Debug.WriteLine(e.exception.Message)
            'SetStatus1(False)
            'CommandReceive(Nothing)
        End If
        If e.eventType = TCPListener.TCPEventArgs.TcpEventType.Timeout Then
            Debug.WriteLine("timeout受信")
            'SetStatus1(False)
            'CommandReceive(Nothing)
        End If
        If e.eventType = TCPListener.TCPEventArgs.TcpEventType.Cancel Then
            Debug.WriteLine("Cancel受信")
            'SetStatus1(False)
            'CommandReceive(Nothing)
        End If
        If e.eventType = TCPListener.TCPEventArgs.TcpEventType.Retry Then
            Debug.WriteLine("retry受信")
            'm_cEquipCommandControl.CommandDisConnect()
        End If
    End Sub
End Class
